package com.weactivity.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weactivity.dto.MessageDTO;
import com.weactivity.dto.SendMessageDTO;
import com.weactivity.entity.Activity;
import com.weactivity.entity.Message;
import com.weactivity.entity.Resource;
import com.weactivity.mapper.MessageMapper;
import com.weactivity.service.MessageService;
import com.weactivity.utils.MessageConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private MessageMapper messageMapper;



    /**
     * 从数据库中查找用户id为userId的未接收消息个数
     * @param userId
     * @return
     */
    @Override
    public int findUnReadById(Long userId) {
        int count =  messageMapper.findUnReadByID(userId);
        return count;
    }

    //todo 优化
    //感觉速度太满了，三条信息需要900ms
    //使用redis?
    @Override
    public List<MessageDTO> findAllById(Long userId) {

        List<MessageDTO> allById = messageMapper.findAllById(userId);
        return allById;
    }

    @Override
    public int  deleteById(int id) {

        int isDelete = messageMapper.removeById(id);
        return isDelete;
    }


    @Override
    public int sendAllMessage(SendMessageDTO sendMessageDTO) {

        List<Integer> idList = sendMessageDTO.getReceiveIdList();
        int sUid = sendMessageDTO.getSendId();
        //System.out.println("sUid" + sUid);
        String subject = sendMessageDTO.getSubject();
        String content = sendMessageDTO.getContent();

        int type = MessageConstant.RECEIVE_MESSAGE;
        int read = MessageConstant.UNREAD;

        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String sTime = localDateTime.format(dateTimeFormatter);
        int sum = 0;
        for(int i  = 0; i < idList.size() ; i ++ ){
            int rUid = idList.get(i);
            int isSuccess = messageMapper.addSingleMessage(sUid,rUid,type,read,sTime,content,subject);
            sum = sum + isSuccess;
        }
        return sum;
    }

    /**
     * 将消息已读
     * @param nid
     * @return
     */

    @Override
    public int isRead(int nid) {

        int count =  messageMapper.isRead(nid);
        return count;
    }

    /**
     * 给活动组织者发送该活动已被更高级的活动占领的消息
     * @param conficetedActOrgId
     * @param confictedActivity
     * @param confictedResource
     * @param similarResourceList
     * @return
     */

    @Override
    public int sendConfictedMessageToOrg(int conficetedActOrgId, Activity confictedActivity, Resource confictedResource, List<Resource> similarResourceList) {

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append("您的活动"+confictedActivity.getName());
        stringBuilder.append(confictedActivity.getBeginTime()+"至"+confictedActivity.getEndTime());
        stringBuilder.append("预约的资源"+confictedActivity.getName());
        stringBuilder.append("被级别更高的活动占用,系统将为您推荐其他类似资源:");

        if (similarResourceList.size() == 0){
            stringBuilder.append("目前暂无类似资源，请更换资源类型或资源等级");
        }else {
            for (Resource i:similarResourceList
            ) {
                stringBuilder.append(i.getName() +" ") ;
            }
        }

        //
        String content = stringBuilder.toString();
        String mSubject = "活动"+confictedActivity.getName()+"重要提醒";

        int count =  messageMapper.sendConfictedMessageToOrg(conficetedActOrgId,content,mSubject);
        return count;
    }

    @Override
    public int systemSendSingleConfictMessage(Integer userId, String contentSystemMessageToConfictUser, String subjectSystemMessageToConfictUsre, String nowStr) {

        return  messageMapper.systemSendSingleConfictMessage(userId,contentSystemMessageToConfictUser,subjectSystemMessageToConfictUsre,nowStr);

    }


}
